package ch.bfh.abhmcm.SoftwareEngineeringAndDesign.controller.importRFC;

import java.io.File;
import java.util.ArrayList;

import javax.swing.DefaultListModel;

import ch.bfh.abhmcm.SoftwareEngineeringAndDesign.view.mainframe.importRFCPanel.ImportRFCPanel;

public class GenerateImportListThread extends Thread {
	private DefaultListModel listModel;
	private ImportRFCPanel importRFCPanel;
	private File filepath;

	public GenerateImportListThread(ImportRFCPanel importRFCPanel,
			DefaultListModel listModel, File filepath) {
		this.importRFCPanel = importRFCPanel;
		this.listModel = listModel;
		this.filepath = filepath;
	}

	public void run() {
		selectImportFile();
	}

	private void selectImportFile() {
		this.listModel.clear();
		int count = 0;
		this.importRFCPanel.setPath(filepath.getAbsolutePath() + "/");
		if (filepath.isFile()) {
			this.listModel.addElement(filepath.getAbsolutePath());
		} else {
			ArrayList<String> list = new ArrayList<String>();
			for (String files : filepath.list()) {
				list.add(files);
				count++;
			}
			if (count == 0) {
				this.listModel.addElement("keine Dateien gefunden");
			} else {
				for (String files : list) {
					listModel.addElement(files);
				}
			}
		}
	}
}